package it.dgtest.client.crud;

import it.dgtest.client.datasource.ProvinceDataSource;
import it.dgtest.shared.Comune;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.oa.gform.client.GFormBinder;
import com.oa.gform.client.bean.binder.BeanBinder;
import com.oa.gform.client.bean.binder.Mapper;
import com.oa.gform.client.bean.binder.Mapping;
import com.oa.gform.client.service.GFormSuccessAsyncCallback;
import com.oa.gform.client.service.ibatis.GIBatisProxyService;
import com.oa.gform.client.service.ibatis.GIBatisProxyServiceAsync;
import com.oa.gform.client.ui.GFieldHidden;
import com.oa.gform.client.ui.GFieldLabel;
import com.oa.gform.client.ui.GFieldListBox;
import com.oa.gform.client.ui.GFieldSuggestBox;
import com.oa.gform.client.ui.GFieldTextBox;
import com.oa.gform.client.ui.GForm;
import com.oa.gform.client.validator.GValidationCallbackSuccess;
import com.oa.gform.rebind.GBeanBinder;
import com.oa.gform.rebind.GDataSource;
import com.oa.gform.rebind.GValidators;
import com.oa.gform.rebind.GBeanBinder.BindMode;
import com.oa.gform.rebind.GDataSource.LoadOn;

public class CrudTestAllField extends GForm {
	private static CrudTestAllFieldUiBinder uiBinder = GWT.create(CrudTestAllFieldUiBinder.class);
	interface CrudTestAllFieldUiBinder extends UiBinder<Widget, CrudTestAllField> {}

	private static CrudTestAllFieldGFormBinder gFormBinder = GWT.create(CrudTestAllFieldGFormBinder.class);
	interface CrudTestAllFieldGFormBinder extends GFormBinder<Widget, CrudTestAllField> {}

	@UiField Button btnInserisci,btnValidate,btnReimposta;
	
	@UiField GFieldLabel lblNome, lblIdProvincia , lblCap, lblIdProvinciaSuggest;
	
	@UiField 
	@GValidators(GFieldLabelName="lblNome", NotEmpty=true) 
	GFieldTextBox<String> fldNome;
	
	@UiField 
	@GValidators(GFieldLabelName="lblIdProvincia", NotEmpty=true,Pattern="[0-9]['-Z]") 
	@GDataSource(dataSource=ProvinceDataSource.class,loadingMode = LoadOn.CREATE) 
	GFieldListBox<Long> fldIdProvincia;

	@UiField 
	@GValidators(GFieldLabelName="lblIdProvinciaSuggest", NotEmpty=true,Pattern="[0-9]['-Z]") 
	@GDataSource(dataSource=ProvinceDataSource.class,loadingMode = LoadOn.CREATE) 
	GFieldSuggestBox<Long> fldIdProvinciaSuggest;

	
	@UiField 
	@GValidators(GFieldLabelName="lblCap", NotEmpty=true,Pattern="[0-9]") 
	GFieldTextBox<String> fldCap;
	
	GFieldHidden<Long> fldIdComune = new GFieldHidden<Long>();

	Comune comune = new Comune();
	
	@GBeanBinder(mode=BindMode.AUTO,beanInstanceName = "comune")
	BeanBinder<CrudTestAllField, Comune> comuneBinder;
	
	//@GBeanProxy(beanInstanceName="beanComune")
	//BeanProxy<BeanComune> comuneProxy;
	
	/***
	 * Costruttore della form
	 */
	public CrudTestAllField() {
		// Inizializza il tamplate ui.xml
		initWidget(uiBinder.createAndBindUi(this));
		// Inizializza il GFormBinder
		gFormBinder.createAndBindGForm(this);
		fldIdProvincia.getSelectedItem();
		comuneBinder.addMapping(new Mapping(fldIdProvinciaSuggest, "IdProvincia", new Mapper() {
			public void formToBean() {
				comune.setIdProvincia(fldIdProvincia.getFieldValue());
			}
			public void beanToForm() {
				fldIdProvinciaSuggest.setFieldValue(comune.getIdProvincia());
			}
		}));
		provaMapper();
	}

	@UiHandler("btnInserisci")
	void onClickInserisci(ClickEvent e) {
		Window.alert("Hello!");
	}

	@UiHandler("btnValidate")
	void onClickValidate(ClickEvent e) {
		validateAllField(new GValidationCallbackSuccess() {
			public void onValidationEnd() {
				Window.alert("OK");
			}
		});
		getGFields();
	}

	@UiHandler("btnReimposta")
	void onClickReset(ClickEvent e) {
		reset();
	}
	
	@UiHandler("btnRicarica")
	void onClickRicarica(ClickEvent e)  {
		GIBatisProxyServiceAsync<Comune> service = GWT.create(GIBatisProxyService.class);
		service.load(new Comune(1l), new GFormSuccessAsyncCallback<Comune>() {
			protected void onSuccessInternal(Comune result) {
				comune = result;
				comuneBinder.beanToForm();
			}
		});
	}
	
	@UiHandler("btnElimina")
	void onClickElimina(ClickEvent e)  {
		GIBatisProxyServiceAsync<Comune> service = GWT.create(GIBatisProxyService.class);
		service.delete(new Comune(1l), new GFormSuccessAsyncCallback<Boolean>() {
			protected void onSuccessInternal(Boolean result) {
				Window.alert(result+ "");
			}
		});
	}
	
	private void provaMapper() {
		/*
		beanComune = new BeanComune();
		beanComune.setField1("PROVA");
		comuneBinder.beanToForm();*/
		/*
		BeanProxy<BeanComune,Long> comuneProxy = new BeanProxy<BeanComune, Long>();
		comuneProxy.loadById(1l, new BeanProxySendCallback<BeanComune, Long>() {
			public void onResponse(BeanProxyResponse<BeanComune,Long> bean) {
				Window.alert("AhAhAh!");
				
			}
		});*/
		
				
		//comuneProxy.loadById(1l);
		/*
		// Prova con i mapping
		comuneBinder = new BeanBinder<CrudTestAllField, BeanComune>();
		comuneBinder.setBeanInstance(beanComune);
		// ADD Custom mapping
		comuneBinder.addMapping(new Mapping(fldField1, "Field1",new Mapper(){
			public void formToBean() {
				beanComune.setField1(fldField1.getText());
			}
			public void beanToForm() {
				fldField1.setText(beanComune.getField1());
			}
		}));*/
		//comuneBinder.addMapping(fldField2);
		//comuneBinder.addMapping(fldField3);
	}
		
}
